package com.tiny.mirror.service.product.application.query;

import com.api.tiny.mirror.service.product.request.PagePlatformRequest;
import com.api.tiny.mirror.service.product.request.ParamsRequest;
import com.api.tiny.mirror.service.product.response.AbilityVO;
import com.api.tiny.mirror.service.product.response.PlatformAbilityVO;
import com.api.tiny.mirror.service.product.response.PlatformVO;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tiny.mirror.basic.common.code.CommonStatusCode;
import com.tiny.mirror.basic.web.dto.response.BasePageResponse;
import com.tiny.mirror.basic.web.exception.BusinessException;
import com.tiny.mirror.service.product.application.convert.AbilityConvert;
import com.tiny.mirror.service.product.application.convert.PlatformConvert;
import com.tiny.mirror.service.product.enums.PlatformStatusEnum;
import com.tiny.mirror.service.product.enums.PlatformTypeEnum;
import com.tiny.mirror.service.product.infrastructure.entity.db.AbilityEntity;
import com.tiny.mirror.service.product.infrastructure.entity.db.AppEntity;
import com.tiny.mirror.service.product.infrastructure.entity.db.PlatformAbilityRelEntity;
import com.tiny.mirror.service.product.infrastructure.entity.db.PlatformEntity;
import com.tiny.mirror.service.product.infrastructure.repository.db.AbilityMapper;
import com.tiny.mirror.service.product.infrastructure.repository.db.AppMapper;
import com.tiny.mirror.service.product.infrastructure.repository.db.PlatformAbilityRelMapper;
import com.tiny.mirror.service.product.infrastructure.repository.db.PlatformMapper;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;
import javax.annotation.Resource;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

/**
 * @Author: liujsh
 * @Description:
 * @Date: Create in 2023/9/27
 */
@Service
public class PlatformQryService {

  @Resource
  private PlatformMapper platformMapper;
  @Resource
  private PlatformAbilityRelMapper platformAbilityRelMapper;
  @Resource
  private AbilityMapper abilityMapper;
  @Resource
  private AppMapper appMapper;

  public BasePageResponse<PlatformVO> page(PagePlatformRequest request) {
    QueryWrapper<PlatformEntity> queryWrapper = new QueryWrapper<>();
    String name = request.getName();
    queryWrapper.lambda().eq(PlatformEntity::getAppId, request.getAppId());
    if (StringUtils.isNotBlank(name)) {
      queryWrapper.lambda().like(PlatformEntity::getName, name);
    }
    PlatformTypeEnum type = request.getType();
    if (type != null) {
      queryWrapper.lambda().like(PlatformEntity::getType, type);
    }
    PlatformStatusEnum status = request.getStatus();
    if (status != null) {
      queryWrapper.lambda().eq(PlatformEntity::getStatus, status);
    }
    ParamsRequest params = request.getParams();
    if (params != null) {
      if (params.getBeginCreateTime() != null) {
        queryWrapper.lambda().ge(PlatformEntity::getCreateTime, params.getBeginCreateTime());
      }
      if (params.getEndCreateTime() != null) {
        queryWrapper.lambda().le(PlatformEntity::getCreateTime, params.getEndCreateTime());
      }
      if (params.getBeginUpdateTime() != null) {
        queryWrapper.lambda().ge(PlatformEntity::getUpdateTime, params.getBeginUpdateTime());
      }
      if (params.getEndUpdateTime() != null) {
        queryWrapper.lambda().le(PlatformEntity::getUpdateTime, params.getEndUpdateTime());
      }
    }
    IPage<PlatformEntity> iPage = platformMapper.selectPage(
        new Page<>(request.getPage(), request.getPageSize()), queryWrapper
    );
    return BasePageResponse.success(
        iPage.getPages(), iPage.getSize(),
        PlatformConvert.convertEntityListToPlatformVOList(iPage.getRecords()),
        iPage.getTotal()
    );
  }

  public PlatformVO getInfo(Long id) {
    PlatformEntity entity = platformMapper.selectById(id);
    if (Objects.isNull(entity)) {
      throw new BusinessException(CommonStatusCode.DATA_NOT_EXIST.args("platform:" + id));
    }
    return PlatformConvert.convertToPlatformVO(entity);
  }

  public PlatformAbilityVO getAuthAbility(Long id) {
    PlatformAbilityVO platformAbilityVO = new PlatformAbilityVO();
    PlatformVO platformVO = getInfo(id);
    if (platformVO == null) {
      return platformAbilityVO;
    }
    Long appId = platformVO.getAppId();
    AppEntity appEntity = appMapper.selectById(appId);
    if (appEntity != null) {
      platformVO.setAppId(appId);
      platformVO.setAppType(appEntity.getType());
    }
    platformAbilityVO.setPlatform(platformVO);
    List<AbilityEntity> abilityList = abilityMapper.selectList(null);
    if (CollectionUtils.isEmpty(abilityList)) {
      platformAbilityVO.setAbilityList(Collections.emptyList());
      return platformAbilityVO;
    }
    List<PlatformAbilityRelEntity> relList = platformAbilityRelMapper.selectByPlatformId(id);
    List<AbilityVO> roleVOList = AbilityConvert.convertEntityListToAbilityVOList(abilityList);
    if (CollectionUtils.isNotEmpty(relList)) {
      List<Long> abilityIds = relList.stream().map(r -> r.getAbilityId())
          .collect(Collectors.toList());
      for (AbilityVO role : roleVOList) {
        role.setFlag(abilityIds.contains(role.getId()));
      }
    }
    platformAbilityVO.setAbilityList(roleVOList);
    return platformAbilityVO;
  }
}
